package yyy.ab.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import yyy.ab.common.core.BaseServiceImpl;
import yyy.ab.common.core.Query;
import yyy.ab.common.core.ResultBean;
import yyy.ab.common.utils.StringUtils;
import yyy.ab.modules.system.entity.SysRole;
import yyy.ab.modules.system.entity.SysRoleMenu;
import yyy.ab.modules.system.entity.dto.SysRoleMenuAllocationDto;
import yyy.ab.modules.system.entity.dto.SysRoleMenuDto;
import yyy.ab.modules.system.entity.dto.SysRoleSmallDto;
import yyy.ab.modules.system.mapper.SysRoleMapper;
import yyy.ab.modules.system.mapper.SysRoleMenuMapper;
import yyy.ab.modules.system.service.SysRoleService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends BaseServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleMenuMapper roleMenuMapper;

    @Override
    public IPage<SysRole> findPage(Map<String, Object> params) {
        Object keyword = params.get("keyword");

        Page<SysRole> page = new Query<SysRole>(params).getPage();
        QueryWrapper<SysRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isNullOrEmpty(keyword), "code", keyword)
                .or().like(!StringUtils.isNullOrEmpty(keyword), "name", keyword);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<SysRoleMenuDto> getRoleMenuList(Long roleId) {
        List<SysRoleMenuDto> roleMenuDtoList = roleMenuMapper.findDtoByRoleId(roleId);
        return roleMenuDtoList;
    }

    @Override
    @Transactional
    public ResultBean allocation(SysRoleMenuAllocationDto roleMenuAllocationDto) {
        if (roleMenuAllocationDto != null) {
            Map<String, Object> deleteRanxMap = new HashMap<>();
            deleteRanxMap.put("role_id", roleMenuAllocationDto.getRoleId());
            roleMenuMapper.deleteByMap(deleteRanxMap);
            if (roleMenuAllocationDto.getRoleMenuDtoList() != null && roleMenuAllocationDto.getRoleMenuDtoList().size() > 0) {
                List<SysRoleMenu> roleMenuList = new ArrayList<>();
                for (SysRoleMenuDto menuDto : roleMenuAllocationDto.getRoleMenuDtoList()) {
                    SysRoleMenu roleMenu = new SysRoleMenu();
                    roleMenu.setRoleId(roleMenuAllocationDto.getRoleId());
                    roleMenu.setMenuId(menuDto.getId());
                    roleMenuList.add(roleMenu);
                }
                roleMenuMapper.batchInsert(roleMenuList);
            }
        } else {
            return ResultBean.errorMsg("分配对象不能为空！");
        }
        return ResultBean.success();
    }

    @Override
    public List<SysRoleSmallDto> findSmallList(){
        return baseMapper.findSmallList();
    }
}
